package com.zking.web.lp;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.zking.dao.domain.Subscription;
import com.zking.dao.domain.User;
import com.zking.service.lp.SubscBasedao;

@Controller
public class SubscController {
	
	@Autowired
	private SubscBasedao subscBasedao;
	
	@RequestMapping(value = "/Subscs")
	@ResponseBody
	public String Subscs(Subscription subscription) {
		int i = subscBasedao.addSubsc(subscription);
		JSONObject jsonObject = new JSONObject();
		return jsonObject.fluentPut("returnVal", i>0?1:0).toString();
	}
	
	
	
	@RequestMapping(value = "/insertSubscs")
	public ModelAndView mlist(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		String pnS = request.getParameter("pn");
		
		int pn = 0;
		int limit = 5;
		try{
			pn = Integer.parseInt(pnS);
		} catch (Exception e) {
			pn = 1;
		}

		if(pn < 1){
			pn = 1;
		}

		int total = (int)subscBasedao.totalSc() ;
		int pages = total / limit;
		if(total % limit != 0){
			pages++;
		}

		if(pn > pages){
			pn = pages;
		}

		int start = (pn - 1)*limit;//第一个参数
//		limit =start+5; 
		/* List<Object> post= redisUtil.lGet("informationlp", start, limit-1); */
		List<Map<String, Object>> subscslp = subscBasedao.mlist(start, limit);
		if (!subscslp.isEmpty()) {
			request.setAttribute("subscslp", subscslp);
			request.setAttribute("pn", pn);
			request.setAttribute("limit", limit);
			request.setAttribute("pages", pages);
//		List<Map<String, Object>> subscslp = subscBasedao.mlist(start, start);
		
//		modelAndView.addObject("subscslp", subscslp);
//		System.out.println(subscslp);
		modelAndView.setViewName("mList");
		}
		return modelAndView;
	}
	
	@RequestMapping(value = "/insertSublp")
	public ModelAndView totalSc2(HttpServletRequest request,User user,Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		String dingyue = subscBasedao.totalSc2();
		//User userlpl = subscBasedao.userlp(id);
		System.out.println("id"+id);
		//String huiyuan = userlpl.getRoleid();
		//System.out.println("huiyuan"+huiyuan);
		//System.out.println("dingyue"+dingyue);
		if(id==3) {
			if(dingyue != null) {
				modelAndView.addObject("dingyue", dingyue);
				modelAndView.setViewName("mList");
			}else {
				modelAndView.setViewName("subscribe");
			}
		}else if(id==2) {
			modelAndView.setViewName("voucher");
		}
		return modelAndView;
	}
}
